package com.pj.compose.ui.compose

import androidx.annotation.DrawableRes
import androidx.compose.animation.*
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.*
import androidx.compose.ui.window.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun LoadingTip(
    show: MutableState<Boolean>,
    width: Dp = 130.dp,
    height: Dp = 120.dp,
    during: Int = 2000,
    msg: String = ""
) {
    BaseTipDialog(show = show, width = width, height = height, during = during) {
        BaseTipContent(msg = msg) {
            CircularProgressIndicator()
        }
    }
}

@Composable
fun MessageTip(
    show: MutableState<Boolean>,
    width: Dp = 130.dp,
    height: Dp = 120.dp,
    during: Int = 1000,
    msg: String = "",
    @DrawableRes iconId: Int? = null
) {
    BaseTipDialog(show = show, width = width, height = height, during = during) {
        BaseTipContent(msg = msg) {
            if (iconId != null)
                Image(painter = painterResource(id = iconId), contentDescription = "icon")
        }

    }
}

@Composable
fun BaseTipDialog(
    show: MutableState<Boolean>,
    width: Dp = 150.dp,
    height: Dp = 150.dp,
    during: Int = 1500,
    content: @Composable (() -> Unit) = {}
) {
    val scope = rememberCoroutineScope()
    TipPopup(show) {
        Surface(
            modifier = Modifier
                .size(width = width,height = height)
                .alpha(0.5f)
                .onSizeChanged {
                    if (during > 0) {
                        scope.launch {
                            delay(during.toLong())
                            show.value = false
                        }
                    }
                },
            color = Color.Black,
            shape = RoundedCornerShape(20.dp)
        ) {
            content()
        }
    }
}

@Composable
fun BaseTipContent(msg: String, topView: @Composable (() -> Unit) = {}) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(10.dp),
        contentAlignment = Alignment.Center
    ) {
        Column(
            verticalArrangement = Arrangement.spacedBy(15.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            topView()
            if (msg.isNotEmpty())
                Text(text = msg, color = Color.White, fontSize = 16.sp)
        }
    }
}

@Composable
fun TipDialog(
    show: MutableState<Boolean>,
    dismissOnClickOutside: Boolean = true,
    content: @Composable (() -> Unit),
) {
    if (show.value)
        Dialog(
            onDismissRequest = { show.value = false },
            properties = DialogProperties(dismissOnClickOutside = dismissOnClickOutside),
            content = content
        )
}

@Composable
fun TipPopup(
    show: MutableState<Boolean>,
    offset: DpOffset = DpOffset(0.dp, 0.dp),
    properties: PopupProperties = PopupProperties(focusable = true),
    content: @Composable (() -> Unit),
) {
    AnimatedVisibility(visible = show.value) {
        val density = LocalDensity.current
        val popupPositionProvider = object : PopupPositionProvider {
            override fun calculatePosition(
                anchorBounds: IntRect,
                windowSize: IntSize,
                layoutDirection: LayoutDirection,
                popupContentSize: IntSize
            ): IntOffset {
                //居中显示
                val x =
                    (windowSize.width - popupContentSize.width - with(density) { offset.x.toPx() }) / 2
                val y =
                    (windowSize.height - popupContentSize.height - with(density) { offset.y.toPx() }) / 2
                return IntOffset(x = x.toInt(), y = y.toInt())
            }
        }

        Popup(
//            onDismissRequest = { show.value = false },
            popupPositionProvider = popupPositionProvider,
            properties = properties,
            content = content
        )
    }
}